﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace PrisonStep
{
    public class Gort
    {
        #region MemberVariables

        private enum States { Stance, StanceStart, Walkstart, WalkLoopStart, Walkloop, Salute, Catch, Eat};

        /// <summary>
        /// The animation model for the aliens
        /// </summary>
        private AnimatedModel mModel;

        /// <summary>
        /// Our Game
        /// </summary>
        private PrisonGame mGame;

        /// <summary>
        /// Player location in the prison. Only x/z are important. y still stay zero
        /// unless we add some flying or jumping behavior later on.
        /// </summary>
        private Vector3 location = new Vector3(0, 0, 0);//(275, 0, 1053);

        /// <summary>
        /// The player orientation as a simple angle
        /// </summary>
        private float orientation = 1.6f;

        /// <summary>
        /// The player transformation matrix. Places the player where they need to be.
        /// </summary>
        private Matrix transform;

        /// <summary>
        /// The rotation rate in radians per second when player is rotating
        /// </summary>
        private float panRate = 2;

        /// <summary>
        /// The player move rate in centimeters per second
        /// </summary>
        private float moveRate = 500;

        /// <summary>
        /// Our initial state
        /// </summary>
        private States mState = States.Stance;

        private BoundingCylinder mBoundingCyl = null;

        #endregion

        #region Properties

        public BoundingCylinder BoundingCyl { get { ComputeBoundingCylinder();  return mBoundingCyl; } }

        public float SlimeWeight { get { return mModel.SlimeWeight; } set { mModel.SlimeWeight = value; } }

        #endregion

        public Gort(PrisonGame aGame)
        {
            this.mGame = aGame;
            mModel = new AnimatedModel(this.mGame, "Gort");
            mModel.AddAssetClip("catcheat", "Gort-catcheat");
            mModel.AddAssetClip("stance", "Gort-stance");
            mModel.AddAssetClip("salute", "Gort-salute");
            mModel.AddAssetClip("walkloop", "Gort-walkloop");
            mModel.AddAssetClip("walkstart", "Gort-walkstart");
            SetPlayerTransform();
            mModel.Skinned = true;

            mBoundingCyl = new BoundingCylinder(location, 192, 30);
        }
        private void SetPlayerTransform()
        {
            transform = Matrix.CreateRotationY(orientation);
            transform.Translation = location;
        }


        public void LoadContent(ContentManager aContent)
        {
            mModel.LoadContent(aContent);
        }

        /// <summary>
        /// This function is called to update this component
        /// to the current game time
        /// </summary>
        /// <param name="aGameTime"></param>
        public void Update(GameTime aGameTime)
        {
            double lDeltaTotal = aGameTime.ElapsedGameTime.TotalSeconds;

            do
            {
                double lDelta = lDeltaTotal;

                Vector3 lInterVect = new Vector3();
                bool lIncomingPie = mGame.Victoria.BazookaProp.PieFire.IncomingPie(BoundingCyl, ref lInterVect);

                switch (mState)
                {
                    case States.StanceStart:
                    {
                        if (lIncomingPie)
                        {
                            mModel.PlayClip("catcheat").Speed = 2;
                            mState = States.Catch;
                        }
                        else
                        {
                            location.Y = 0;
                            mModel.PlayClip("stance");
                            mState = States.Stance;
                        }
                        break;
                    }


                    case States.Stance:
                    {
                        mModel.PlayClip("walkstart");
                        mState = States.Walkstart;

                        break;
                    }
                    case States.Walkstart:
                    {
                        if (mModel.FinishedAnimation())
                        {
                            location.Y = 0;
                            mModel.PlayClip("walkloop");
                            mState = States.WalkLoopStart;
                        }

                        break;
                    }
                    case States.WalkLoopStart:
                    {

                        break;
                    }
                    case States.Walkloop:
                    {

                        break;
                    }
                    case States.Salute:
                    {

                        break;
                    }
                    case States.Catch:
                    {

                        break;
                    }
                    case States.Eat:
                    {

                        break;
                    }
                }

                lDeltaTotal -= lDelta;
            }
            while (lDeltaTotal > 0);
        }

        /// <summary>
        /// This function is called to draw this game component
        /// </summary>
        /// <param name="aGraphics">Devicet to draw the model on.</param>
        /// <param name="aGameTime">Currentgame time.</param>
        /// <param name="aTransform">Transform that puts the model where we want it.</param>
        public void Draw(GraphicsDeviceManager aGraphics, GameTime aGameTime)
        {
            mModel.Draw(aGraphics, aGameTime, transform);
        }

        private void ComputeBoundingCylinder()
        {
            mBoundingCyl.Location = location;
        }
    }
}